package vigenere;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Vigenere {
	
	public static void main(String[] args) {
		
		InputStream inputStream;
		String text = "";
		try {
			inputStream = new FileInputStream("Anhang.txt");
			while(inputStream.available() > 0) text+=(char)inputStream.read();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		System.out.println(text);
		
		
		
		
		String res = decode(text,"GEHEIMNIX");
		System.out.println(res);
	}
	
    public static String decode(String NText, String code) {
        StringBuilder ctext = new StringBuilder();
        
        int a = 0;
        for(int i = 0; i < NText.length(); i++) {
            char key = code.toUpperCase().charAt(a%code.length());
            char aCh = NText.charAt(i);
            char nCh = ' ';
            
            if(aCh >= 65 && aCh <= 90) {
                nCh = (char)(aCh + 65 - key);
                if((int)nCh < 65){
                    nCh += 26;
                }
                if(aCh >= 97){
                    nCh += 32;
                }
                ctext.append(nCh);
                a++;
            }else if(aCh >= 97 && aCh <= 122){
                nCh = (char)(aCh + 65 - key);
                if((int)nCh < 97){
                    nCh += 26;
                }
                ctext.append(nCh);
                a++;
            }else{
                ctext.append(aCh);
            }
        }
        return ctext.toString();
    }

}
